-module(ejieba_app).

-behaviour(application).


-export([start/2, stop/1]).

-export([
    start/0,
    stop/0
]).

start(_StartType, _StartArgs) ->
    
    start_http(),
    
    ejieba_sup:start_link().

stop(_State) ->
    ok.


start_http() ->
    application:start(ranch),
    application:start(cowlib),
    application:start(cowboy),
    
    Dispatch = cowboy_router:compile([
        {'_', [
            {"/", cowboy_static, {file, "priv/docroot/index.html"}},
            {"/segment/[...]", ejieba_http, []}
        ]}
    ]),
    
    HttpPort = 8010,
    {ok, Pid} = cowboy:start_clear(http, 16, [{port, HttpPort}], #{
        env => #{dispatch => Dispatch},
        request_timeout => 10000
    }),
    io:format("http port:~p... pid:~p started~n", [HttpPort, Pid]).


start() ->
    application:start(crypto),
    application:start(inets),
    ssl:start(),
    application:start(ejieba).


stop() ->
    application:stop(ejieba),
    init:stop().